/*
 *  Copyright (C) 1993  Robert Davis
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of Version 2, or any later version, of 
 *  the GNU General Public License as published by the Free Software 
 *  Foundation.
 */


static char RCSId[]="$Id: Pane.m,v 1.10 1993/05/18 03:55:32 davis Exp $";


#import <appkit/Application.h>
#import <appkit/Matrix.h>
#import <appkit/Text.h>			/* Pane's can be text delegates */
#import <appkit/TextFieldCell.h>

#import <objc/NXStringTable.h>

#import "Pane.h"


@implementation Pane


- init
{
    [super init];

    icon = NULL;
    isCurrentPane = NO;
    _textChanged = NO;

    return self;
}


- free
{
    NXZoneFree ([self zone], title);
    [window free];
    if (view)
	[view free];
    [stringSet free];

    return [super free];
}


- awakeFromNib
{
    view = [window setContentView:[[View alloc] init]];
    [self setTitle: [stringSet valueForStringKey:"title"]];
    return self;
}


- view
{
    return view;
}



- (const char *) icon
{
    return icon;
}


- setTitle: (const char *) aString
{
    NXZone *zone;

    NXZoneFree (zone = [self zone], title);

    if (aString)
	title = NXCopyStringBufferFromZone (aString, zone);
    else
	title = NULL;

    return self;
}

	
- (const char *) title
{
    return title;
}


- selectControl:sender
{
    return nil;
}


/*  
 *  Returns YES if the pane has changed and the view needs to be 
 *  redisplayed.
 */
- (BOOL)updateStatus:aStatus doc:aDoc
{
    status = aStatus;
    doc = aDoc;
    return (aStatus && aDoc);
}


/*  
 *  Returns YES if the pane has changed and the view needs to be 
 *  redisplayed.
 */
- (BOOL)forceUpdateStatus:aStatus doc:aDoc
{
    status = nil;
    doc = nil;

    if (isCurrentPane)
	return [self updateStatus:aStatus doc:aDoc];
    else
	return NO;
}



- didSwapIn:sender
{
    isCurrentPane = YES;
    return self;
}


- didSwapOut:sender
{
    isCurrentPane = NO;
    return self;
}


- (BOOL) isCurrentPane
{
    return isCurrentPane;
}


- textDidEnd:textObject endChar:(unsigned short)whyEnd
{
    /*  
     *  Normally, a text-based control's action is sent only when 
     *  editting ends with a Return.  We use this method to send the 
     *  action on Tab and Backtab, too.
     */

    if (_textChanged && ((whyEnd == NX_TAB) || (whyEnd == NX_BACKTAB)))  {

	id	control = [textObject delegate];
	Cell	*editCell;

	/*  
	 *  If the Control is a Matrix, we check to see if the Cell 
	 *  being edited has an action, in which case we send it to 
	 *  the Cell's target.  Otherwise send the Control's action to 
	 *  the Control's target.
	 */
	
	if ([control isKindOf:[Matrix class]] &&
	    [(editCell = [control selectedCell]) action])

	    [[editCell controlView] sendAction:[editCell action]
					    to:[editCell target]];

	else			/* Otherwise, do the control's action	*/

	    [control sendAction:[control action] to:[control target]];

    }

    _textChanged = NO;

    return self;
}



- (BOOL) textWillChange:textObject
{
    _textChanged = YES;
    return NO;		/* NO means it's okay for the text to change	*/
}


// Shuts up the compiler about unused RCSId
- (const char *) rcsid
{
    return RCSId;
}


@end
